package yl.hs.bmipfull.controllers.construction;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.pojo.production.BrickShipOrderModel;
import yl.hs.bmipfull.service.production.IBrickShipService;
import yl.hs.bmipfull.service.production.IProductShipOrderService;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.WebResult;
import yl.hs.bmipfull.viewmodels.salemanmage.ConstructionSignModel;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/*
 * ConstructionShipOrderController 工地发货单信息
 * @category 销售管理
 * @author 谢景广
 * @date 2022-04-21
 */
@RequestMapping("/construction/shipOrders")
@Api(value = "工地系统", tags = "工地系统——发货单管理")
@RestController
public class ConstructionShipOrderController extends ControllerBase {

    @Autowired
    IProductShipOrderService orderService;
    @Autowired
    IBrickShipService brickShipService;
    @PostMapping("query/{rowNum}/{page}" )
    @ApiOperation(value = "查询所有发货单信息", notes = "分页查询所有发货单信息" )
    public WebResult queryOrderList(@PathVariable("rowNum")int rowNum, @PathVariable("page")int page, @RequestBody PageQuery query){
        try {
            var result = brickShipService.queryModelPageByContractListSN(query,rowNum,page);
            var allList=brickShipService.queryModelPageByContractListSN(query,100000,1).getData();
            var map= Tl.toMap(result);
            var sumNetWeight=allList.stream().mapToDouble(s -> Double.valueOf((s.getQuantity()==null ? 0:s.getQuantity()).toString())).sum();
            var sumAmount=allList.stream().mapToDouble(s -> Double.valueOf((s.getVolume()==null ? 0:s.getVolume()).toString())).sum();
            map.put("sumNetWeight",sumNetWeight);
            map.put("sumVolime",sumAmount);
            return success("查询成功", map);
        }
        catch (Exception ee){
            return error(ee);
        }
    }
    @PostMapping("query/custom")
    @ApiOperation(value = "查询所有发货单信息", notes = "分页查询所有发货单信息" )
    public WebResult queryOrderList(@RequestBody PageQuery query){
        try {
            List<Object> result = new ArrayList<>();
            var allList=brickShipService.queryModelPageByContractListSN(query,100000,1).getData();
            var maps = allList.stream().collect(Collectors.groupingBy(BrickShipOrderModel::getProductName));
            maps.keySet().forEach(s->{
                Map<String,Object> entity = new HashMap<>();
                entity.put("productName",maps.get(s).get(0).getProductCate()+"-"+s+"-"+maps.get(s).get(0).getProductSpec());
                entity.put("weight",maps.get(s).stream().map(BrickShipOrderModel::getQuantity).reduce(BigDecimal::add));
                entity.put("quantity",maps.get(s).stream().map(BrickShipOrderModel::getVolume).reduce(BigDecimal::add));
                result.add(entity);
            });
            return success("查询成功", result);
        }
        catch (Exception ee){
            return error(ee);
        }
    }
    @RequestMapping(value = "getNotSignedCount",method = RequestMethod.GET)
    @ApiOperation(value = "查询未签收发货单信息", notes = "查询未签收发货单信息" )
    public WebResult queryNotSignedContractOrderCount(){
        try {
            var result = orderService.queryNotSignedContractOrderCount(new PageQuery());
            return success("查询成功", result);
        }
        catch (Exception ee){
            return error(ee);
        }
    }

    @PostMapping("sign")
    @ApiOperation(value = "保存签收信息", notes = "保存用户提交的签收信息" )
    public WebResult signShipOrder(@ApiParam(value = "提交信息") @RequestBody ConstructionSignModel model){
        try{
            var result = orderService.saveConstructionSignInfo(model);
            if(result>0) {
                return success("签收成功");
            }
            else{
                return fail("签收失败");
            }
        }
        catch (Exception ee){
            return error(ee);
        }
    }
}
